/**
 * 2018年12月16日上午7:00:20
 * @author 谢进财
 */
package com.baihang.project.ds.webSocket.interceptor;

import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.server.ServerHttpRequest;
import org.springframework.http.server.ServerHttpResponse;
import org.springframework.web.socket.WebSocketHandler;
import org.springframework.web.socket.server.HandshakeInterceptor;

import com.baihang.framework.kit.ShiroKit;

/**
 * websocket握手阶段。是由http协议升级到websocket协议。此时还是可以获取request,response
 *
 */
public class DsWebSocketInterceptor implements HandshakeInterceptor {
	private static Logger logger = LoggerFactory.getLogger(DsWebSocketInterceptor.class);
	/* (non-Javadoc)
	 * @see org.springframework.web.socket.server.HandshakeInterceptor#beforeHandshake(org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse, org.springframework.web.socket.WebSocketHandler, java.util.Map)
	 */
	@Override
	public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Map<String, Object> attributes) throws Exception {
			logger.info("握手");
			if(ShiroKit.isAuthed()) {
				logger.info("{}连上来了",ShiroKit.getCurrentUser().getUserName());
				attributes.put("userName", ShiroKit.getCurrentUser().getUserName());				
			}else {
				logger.info("匿名用户连接websocket");
				attributes.put("userName", "ds");//针对首页未登录状态，进行匿名Ds
			}
        return true;
	}

	/* (non-Javadoc)
	 * @see org.springframework.web.socket.server.HandshakeInterceptor#afterHandshake(org.springframework.http.server.ServerHttpRequest, org.springframework.http.server.ServerHttpResponse, org.springframework.web.socket.WebSocketHandler, java.lang.Exception)
	 */
	@Override
	public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler,
			Exception exception) {
		// TODO Auto-generated method stub

	}

}
